<?php
/**
 * Created by PhpStorm.
 * User: clkj1
 * Date: 2018/6/19
 * Time: 9:37
 */
require 'core/header.php';
if (!isset($_SESSION['uid'])) {
    exit('<a href="login.php?back=\'' . base64_encode($url) . '\'">请登录</a>');
}
if (isset($_GET['action'])) {
    if ($_GET['action'] == "newtopic") {
        addtopic();
    } elseif ($_GET['action'] == "edittopic") {
        edittopic();
    }
}

function edittopic()
{
    $forums = sql_topicid_forums($_GET['tid']);
    $forum_auth_post = sql_forumid_forums($forums['forum_id']);
    $forum_auth_post = $forum_auth_post['auth_post'];
    $topics = sql_topicid_topics($_GET['tid']);
    $topic_title = htmlspecialchars($topics['topic_title']);
    $topic_posts = sql_topicid_posts($_GET['tid']);
    $topic_content = $topic_posts['post_content'];
    $fid = $_GET['fid'];
    $forums = sql_forumid_forums($fid);
    if ($_SESSION['rank'] == 10 || $_SESSION['rank'] == $forums['auth_edit'] || $_SESSION['uid'] == $topics['topic_poster']) {
        echo '<h3>编辑帖子：</h3>';
        $user_all = sql_search_uid_all($_SESSION['uid']);
        if ($user_all['user_level'] >= $forum_auth_post) {
            ?>
            帖子主题：<input type="text" id="topic_title" value="<?= $topic_title ?>"/>
            <?php
            if ($topics['topic_type'] == 1) {
                ?>
                <select id="select_type">
                    <option value="1" selected="selected">求助</option>
                    <option value="2">已解决</option>
                </select>
                <?php
            } elseif ($topics['topic_type'] == 2) {
                ?>
                <select id="select_type">
                    <option value="1">求助</option>
                    <option value="2" selected="selected">已解决</option>
                </select>
                <?php
            }

//        foreach ( AS ){
//
//        }
            ?>
            是否置顶：
            <?php
            if ($_SESSION['rank'] == 10 || $_SESSION['rank'] == $forums['auth_edit']) {
                if ($topics['topic_top'] == 1)
                    echo '<input id="settop" type="checkbox" checked="checked"/>';
                elseif ($topics['topic_top'] == 0)
                    echo '<input id="settop" type="checkbox"/>';
            } else {
                echo '<input id="settop" type="checkbox" disabled="disabled"/>';
            }
            ?>
            <div id="area">
                移动到：
                <select id="selectForum">
                    <?php
                    $search_area_asc = search_bbs_area_asc();
                    while (($area_array = mysqli_fetch_array($search_area_asc)) != false) {
                        ?>
                        <br/>
                        <option disabled="disabled">论坛区：<?= $area_array['area_title'] ?></option>
                        <?php
                        $result = sql_areaid_forum($area_array['area_id']);
                        while (($forum_array = mysqli_fetch_array($result)) != false) {
                            if ($forum_array['forum_id'] == $_GET['fid'])
                                echo '<option value="' . $forum_array['forum_id'] . '" selected="selected">' . $forum_array['forum_name'] . '</option>';
                            else
                                echo '<option value="' . $forum_array['forum_id'] . '">' . $forum_array['forum_name'] . '</option>';
                        }
                    }
                    ?>
                </select>

            </div>
            <br/><br/>
            帖子内容：<br/><br/>
            <!-- 加载编辑器的容器 -->
            <script id="container" name="content" type="text/plain"><?= $topic_content ?></script>
            <!-- 配置文件 -->
            <script type="text/javascript" src="utf8-php/ueditor.config.js"></script>
            <!-- 编辑器源码文件 -->
            <script type="text/javascript" src="utf8-php/ueditor.all.js"></script>
            <!-- 实例化编辑器 -->
            <script type="text/javascript">
                var ue = UE.getEditor('container');
            </script>
            <!-- 隐藏的post -->
            <input type="hidden" id="topicid" value="<?= $_GET['tid'] ?>"/>
            附加选项：
            <input type="checkbox" id="vote"/>是否投票
            <br/><br/>
            <?php
            $back_url = isset($_GET['back']) ? $_GET['back'] : "0";
            ?>
            <button type="button"
                    onclick="ajaxpost_edittopic(<?= $_GET['fid'] ?>, <?= $_GET['tid'] ?>, '<?= $back_url ?>')">编辑帖子
            </button>
            <?php
            if (!isset($_GET['back'])) {
                ?>
                <a href="#" onclick="javascript:history.go(-1);">返回</a>
                <?php
            } else {
                ?>
                <a href="<?= base64_decode($_GET['back']) ?>">返回</a>
                <?php
            }
        }
    } else {
        echo "<script>alert('非法访问，返回上一页'); history.go(-1);</script>";
    }
}


function addtopic()
{

    $forum_auth_post = sql_forumid_forums($_GET['fid']);
    $forum_auth_post = $forum_auth_post['auth_post'];

    echo '<h3>发表帖子：</h3>';
    $user_all = sql_search_uid_all($_SESSION['uid']);
    if ($user_all['user_level'] >= $forum_auth_post) {
        ?>
        帖子主题：<input type="text" id="topic_title"/><br/><br/>
        帖子内容：<br/><br/>
        <!-- 加载编辑器的容器 -->
        <script id="container" name="content" type="text/plain">
            <p></p><?= htmlspecialchars_decode($user_all['user_isignatur']) ?></script>
        <!-- 配置文件 -->
        <script type="text/javascript" src="utf8-php/ueditor.config.js"></script>
        <!-- 编辑器源码文件 -->
        <script type="text/javascript" src="utf8-php/ueditor.all.js"></script>
        <!-- 实例化编辑器 -->
        <script type="text/javascript">
            var ue = UE.getEditor('container');
        </script>
        <!-- 隐藏的post -->
        <input type="hidden" id="forumid" value="<?= $_GET['fid'] ?>"/>
        附加选项：
        <input type="checkbox" id="vote" value="1"/>是否投票
        <br/><br/>
        <button type="button" onclick="ajaxpost_addtopic()">发表帖子</button>
        <?php
        if (!isset($_GET['back'])) {
            ?>
            <a href="#" onclick="javascript:history.go(-1);">返回</a>
            <?php
        } else {
            ?>
            <a href="<?= base64_decode($_GET['back']) ?>">返回</a>
            <?php
        }
    } else {
        echo "发帖需要等级：<u>" . $forum_auth_post . "</u>，您的等级为：<u>" . $user_all['user_level'] . "</u>，请尽快提升等级！";
    }
}

require 'core/footer.php';
?>